package tmhm.components;

import java.io.*;

public class MicroMEM extends LogicalComponent {

    private String[] memory;

    public MicroMEM(int size) {
    	super(1,6,false);
		
		out[0].setIsInt();
		out[0].setNumOfLines(24);
		out[1].setIsInt();
		out[1].setNumOfLines(24);
		out[2].setIsInt();
		out[2].setNumOfLines(24);
		out[3].setIsInt();
		out[3].setNumOfLines(24);
		out[4].setIsInt();
		out[4].setNumOfLines(8);
		out[5].setIsInt();
		out[5].setNumOfLines(8);
		memory = new String[size];

        int counter = 0;
        BufferedReader reader=null;
        try {
            reader = new BufferedReader(new FileReader("conf/tmhm/microkod.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                memory[counter++] = line;
            }
        } catch (Exception e) {
        }finally{
			if(reader!=null)
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
        
    }

    @Override
    public void func() {
    	 System.out.println( memory[in[0].getIntVal()] );
    	out[0].setIntVal( Integer.parseInt( memory[in[0].getIntVal()].substring(0,6), 16 ));
    	out[1].setIntVal( Integer.parseInt( memory[in[0].getIntVal()].substring(6,12), 16 ));
    	out[2].setIntVal( Integer.parseInt( memory[in[0].getIntVal()].substring(12,18), 16 ));
    	out[3].setIntVal( Integer.parseInt( memory[in[0].getIntVal()].substring(18,24), 16 ));
		out[4].setIntVal( Integer.parseInt( memory[in[0].getIntVal()].substring(24,26), 16 ));
		out[5].setIntVal( Integer.parseInt( memory[in[0].getIntVal()].substring(26,28), 16 ));
    }
    public String getAllOut(){
        return memory[in[0].getIntVal()];
    }

    public static void main(String[] args) {

        MicroMEM mm = new MicroMEM(1024);

        mm.setInputPin(0, new Pin(71, 8));

        mm.func();

        System.out.println( Integer.toBinaryString(mm.getOut(0).getIntVal()) );
		System.out.println( Integer.toBinaryString(mm.getOut(1).getIntVal()) );
		System.out.println( Integer.toBinaryString(mm.getOut(2).getIntVal()) );
		System.out.println( Integer.toBinaryString(mm.getOut(3).getIntVal()) );
		System.out.println( Integer.toBinaryString(mm.getOut(4).getIntVal()) );
		System.out.println( Integer.toBinaryString(mm.getOut(5).getIntVal()) );
    }
}
